package org.darcy.service.business.impl;

import java.util.List;

import org.darcy.dao.business.BusinessClientMapper;
import org.darcy.entity.business.Address;
import org.darcy.entity.business.BankInfo;
import org.darcy.entity.business.BusinessClient;
import org.darcy.entity.business.Manager;
import org.darcy.framework.generic.GenericServiceImpl;
import org.darcy.framework.model.PageData;
import org.darcy.framework.model.PageParam;
import org.darcy.service.business.BusinessClientService;
import org.darcy.service.sys.SysFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;

/**
 * BusinessClient服务实现
 */
@Service
@Transactional
public class BusinessClientServiceImpl extends GenericServiceImpl<BusinessClient> implements BusinessClientService {

	@Autowired
	private BusinessClientMapper mapper;

	@Autowired
	@Lazy
	private SysFileService SysFileService;

	/**
	 * 处理客户信息
	 */
	private void processData(BusinessClient record) {
		// 处理json
		record.setAddress(JSONUtil.toJsonStr(record.getAddressList()));
		record.setManager(JSONUtil.toJsonStr(record.getManagerList()));
		for (BankInfo a : record.getBankInfoList()) {
			if (StrUtil.isEmpty(a.getId())) {
				a.setId(UUID.fastUUID().toString());
			}
		}
		record.setBankInfo(JSONUtil.toJsonStr(record.getBankInfoList()));
		// 处理附件
		if (record.getFileList() != null && !record.getFileList().isEmpty()) {
			String file = SysFileService.getFileIdList(record.getFileList());
			record.setFile(file);
		}
	}

	@Override
	public BusinessClient insert(BusinessClient record) {
		processData(record);
		super.insert(record);
		return record;
	}

	@Override
	public BusinessClient update(BusinessClient record) {
		processData(record);
		super.update(record);
		return record;
	}

	@Override
	public BusinessClient getDetail(Integer id) {
		BusinessClient record = mapper.selectById(id);
		record.setAddressList(JSONUtil.toList(record.getAddress(), Address.class));
		record.setManagerList(JSONUtil.toList(record.getManager(), Manager.class));
		record.setBankInfoList(JSONUtil.toList(record.getBankInfo(), BankInfo.class));
		// 文件处理
		if (StrUtil.isNotEmpty(record.getFile())) {
			record.setFileList(SysFileService.getFilePathList(record.getFile()));
		}
		return record;
	}

	@Override
	public PageData<BusinessClient> page(PageParam<BusinessClient> param) {
		PageData<BusinessClient> data = super.getPageData(param);
		for (BusinessClient a : data.getRows()) {
			a.setAddressList(JSONUtil.toList(a.getAddress(), Address.class));
			a.setManagerList(JSONUtil.toList(a.getManager(), Manager.class));
			a.setBankInfoList(JSONUtil.toList(a.getBankInfo(), BankInfo.class));
		}
		return data;
	}

	@Override
	public List<BusinessClient> getAll(String keyword) {
		QueryWrapper<BusinessClient> q = new QueryWrapper<BusinessClient>();
		if (!StrUtil.isEmptyIfStr(keyword)) {
			q.like("name", keyword);
		}
		return this.getList(q);
	}

}
